package ua.maxicash.vo;

import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;

import javax.persistence.*;
import javax.validation.constraints.Size;
import java.util.HashSet;
import java.util.Set;

@Entity
@Table(uniqueConstraints = {@UniqueConstraint(columnNames = {"EMAIL"})})
@Inheritance(strategy = InheritanceType.JOINED)
@JsonIgnoreProperties({"password","avatar","likes"})
public abstract class SystemUser {

    @Id
    @Column(name = "ID_USER")
    @GeneratedValue
    protected Long idUser;

    @ManyToOne
    @JoinColumn(name = "id_City")
    protected City city;

    @Column(name = "PASSWORD")
    protected String password;

    @Lob
    @Column(name = "Avatar")
    protected byte[] avatar;

    @Column(name = "Icq")
    protected String icq;

    @Column(name = "Enabled")
    protected Boolean enabled;

    @Column(name = "Skype")
    protected String skype;

    @Column(name = "ABOUT")
    @Size(min = 2)
    protected String about;

    @Column(name = "EMAIL")
    @Size(min = 2)
    private String email;

    @Column(name = "FIRST_NAME")
    @Size(min = 2)
    protected String firstName;

    @Column(name = "LAST_NAME")
    @Size(min = 2)
    protected String lastName;

    @Column(name = "PHONE")
    protected String phone;

    @OneToMany
    @JoinTable(name = "SystemUser_Likes", joinColumns = {@JoinColumn(name = "ID_USER")},
            inverseJoinColumns = {@JoinColumn(name = "ID_FRIEND")})
    @LazyCollection(LazyCollectionOption.FALSE)
    @org.hibernate.annotations.Cascade(value = {org.hibernate.annotations.CascadeType.SAVE_UPDATE})
    private Set<SystemUser> likes = new HashSet<SystemUser>();

    public SystemUser() {
    }

    public Long getIdUser() {
        return idUser;
    }

    public void setIdUser(Long idUser) {
        this.idUser = idUser;
    }

    public Boolean isEnabled() {
        return enabled;
    }

    public void setEnabled(Boolean enabled) {
        this.enabled = enabled;
    }

    public City getCity() {
        return city;
    }

    public void setCity(City city) {
        this.city = city;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public byte[] getAvatar() {
        return avatar;
    }

    public void setAvatar(byte[] avatar) {
        this.avatar = avatar;
    }

    public String getIcq() {
        return icq;
    }

    public void setIcq(String icq) {
        this.icq = icq;
    }

    public String getSkype() {
        return skype;
    }

    public void setSkype(String skype) {
        this.skype = skype;
    }

    public String getAbout() {
        return about;
    }

    public void setAbout(String about) {
        this.about = about;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public abstract Set<JobType> getJobTypes();

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public Set<SystemUser> getLikes() {
        return likes;
    }

    public void setLikes(Set<SystemUser> likes) {
        this.likes = likes;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        SystemUser that = (SystemUser) o;

        if (idUser != null ? !idUser.equals(that.idUser) : that.idUser != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        return idUser != null ? idUser.hashCode() : 0;
    }

    @Override
    public String toString() {
        return "SystemUser{" +
                "email='" + email + '\'' +
                '}';
    }
}
